home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 May / PCPlus May 1998=disk A.iso / full / CBUILDER / SAMS / SAMPLES / CHAP07 / SPMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-12  |  5.2 KB  |  177 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "SPMain.h"
  6. //---------------------------------------------------------------------------
  7. #pragma resource "*.dfm"
  8. TScratchPad *ScratchPad;
  9. //---------------------------------------------------------------------------
  10. __fastcall TScratchPad::TScratchPad(TComponent* Owner)
  11.   : TForm(Owner)
  12. {
  13. }
  14. //---------------------------------------------------------------------------
  15. void __fastcall TScratchPad::FileNewClick(TObject *Sender)
  16. {
  17.     //
  18.     // Open a file. First check to see if the current file
  19.   // needs to be saved.
  20.   //
  21.     if (Memo->Modified) {
  22.     //
  23.     // Display a message box.
  24.     //
  25.       int result = Application->MessageBox(
  26.         "The current file has changed. Save changes?",
  27.       "ScratchPad Message", MB_YESNOCANCEL | MB_ICONWARNING);
  28.     //
  29.     // If Yes was clicked then save the currnt file.
  30.     //
  31.        if (result == IDYES) FileSaveClick(Sender);
  32.     //
  33.     // If No was clicked then do nothing.
  34.     //
  35.     if (result == IDCANCEL) return;
  36.   }
  37.   //
  38.   // Delete the strings in the memo, if any.
  39.   //
  40.   if (Memo->Lines->Count > 0) Memo->Clear();
  41.   //
  42.   // Set the FileName property of the Save Dialog to a
  43.   // blank string. This lets us know that the file has
  44.   // not yet been saved.
  45.   //
  46.   SaveDialog->FileName = "";
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TScratchPad::FileOpenClick(TObject *Sender)
  50. {
  51.     //
  52.     // Open a file. First check to see if the current file needs
  53.   // to be saved. Same logic as in FileNewClick() above.
  54.   //
  55.     if (Memo->Modified) {
  56.       int result = Application->MessageBox(
  57.         "The current file has changed. Save changes?",
  58.       "ScratchPad Message", MB_YESNOCANCEL | MB_ICONWARNING);
  59.        if (result == IDYES) FileSaveClick(0);
  60.     if (result == IDCANCEL) return;
  61.   }
  62.   //
  63.   // Execute the File Open dialog. If OK was pressed then
  64.   // open the file using the LoadFromFile() method. First
  65.   // clear the FileName property.
  66.   //
  67.   OpenDialog->FileName = "";
  68.   if (OpenDialog->Execute())
  69.     {
  70.       if (Memo->Lines->Count > 0) Memo->Clear();
  71.          Memo->Lines->LoadFromFile(OpenDialog->FileName);
  72.     SaveDialog->FileName = OpenDialog->FileName;
  73.   }
  74. }
  75. //---------------------------------------------------------------------
  76. void __fastcall TScratchPad::FileSaveClick(TObject *Sender)
  77. {
  78.     //
  79.   // If a filename has already been provided then there is
  80.   // no need to bring up the File Save dialog. Just save the
  81.   // file using SaveToFile().
  82.   //
  83.     if (SaveDialog->FileName != "")
  84.   {
  85.       Memo->Lines->SaveToFile(SaveDialog->FileName);
  86.     //
  87.     // Set Modified to false since we've just saved.
  88.     //
  89.     Memo->Modified = false;
  90.   }
  91.   //
  92.   // If no filename was set then do a SaveAs().
  93.   //
  94.     else FileSaveAsClick(Sender);
  95. }
  96. //---------------------------------------------------------------------
  97. void __fastcall TScratchPad::FileSaveAsClick(TObject *Sender)
  98. {
  99.   //
  100.   // Display the File Save dialog to save the file.
  101.   // Set Modified to false since we just saved.
  102.   //
  103.     SaveDialog->Title = "Save As";
  104.     if (SaveDialog->Execute())
  105.   {
  106.       Memo->Lines->SaveToFile(SaveDialog->FileName);
  107.     Memo->Modified = false;
  108.   }
  109. }
  110. //---------------------------------------------------------------------
  111. void __fastcall TScratchPad::FileExitClick(TObject *Sender)
  112. {
  113.     //
  114.   // All done. Close the form.
  115.   //
  116.     Close();
  117. }
  118. //---------------------------------------------------------------------
  119. void __fastcall TScratchPad::EditUndoClick(TObject *Sender)
  120. {
  121.     //
  122.   // TMemo doesn't have an Undo method so we have to send
  123.   // a Windows WM_UNDO message to the memo component.
  124.   //
  125.     SendMessage(Memo->Handle, WM_UNDO, 0, 0);
  126. }
  127. //---------------------------------------------------------------------
  128. void __fastcall TScratchPad::EditSelectAllClick(TObject *Sender)
  129. {
  130.     //
  131.   // Just call TMemo::SelectAll().
  132.   //
  133.     Memo->SelectAll();
  134. }
  135. //---------------------------------------------------------------------
  136. void __fastcall TScratchPad::EditCutClick(TObject *Sender)
  137. {
  138.     //
  139.   // Call TMemo::CutToClipboard().
  140.   //
  141.     Memo->CutToClipboard();
  142. }
  143. //---------------------------------------------------------------------
  144. void __fastcall TScratchPad::EditCopyClick(TObject *Sender)
  145. {
  146.     //
  147.   // Call TMemo::CopyToClipboard().
  148.   //
  149.     Memo->CopyToClipboard();
  150. }
  151. //---------------------------------------------------------------------
  152. void __fastcall TScratchPad::EditPasteClick(TObject *Sender)
  153. {
  154.     //
  155.   // Call TMemo::PasteFromClipboard().
  156.   //
  157.     Memo->PasteFromClipboard();
  158. }
  159. //---------------------------------------------------------------------
  160. void __fastcall TScratchPad::EditWordWrapClick(TObject *Sender)
  161. {
  162.     //
  163.   // Toggle the TMemo::WordWrap property. Set the Checked
  164.   // property of the menu item to the same value as WordWrap.
  165.   //
  166.     Memo->WordWrap = !Memo->WordWrap;
  167.   EditWordWrap->Checked = Memo->WordWrap;
  168.   //
  169.   // If WordWrap is on then we only need the vertical scroll
  170.   // bar. If it's off, then we need both scroll bars.
  171.   //
  172.   if (Memo->WordWrap) Memo->ScrollBars = ssVertical;
  173.   else Memo->ScrollBars = ssBoth;
  174. }
  175. //---------------------------------------------------------------------
  176.  
  177.